<?php

/**
 * 创建一个socket客户端
 */
startSocketClient('127.0.0.1', 8080);


function startSocketClient($address, $port)
{
    // 创建一个TCP/IP socket
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    // 连接服务端
    socket_connect($socket, $address, $port);

    // 向服务端发送数据
    $message = "Hello, Server!";
    $messageLength = strlen($message);
    socket_write($socket, $message, $messageLength);

    // 从服务端接收数据
    $response = socket_read($socket, 1024);

    // 处理接收到的数据
    echo "Server Response: " . $response;

    // 关闭socket连接
    socket_close($socket);
}


